@extends('admin.layout.layout', ['row' => @$page])

@section('contain')

    <style>
        .table .tth{
            background-color: rgba(0, 0, 0, 0.05);
            width: 150px;
            font-weight: bold;
        }
        .table .ttd{
            width: 350px;
        }
        .table .ttt{
            padding: 0;
        }
        .ttt .table{
            margin: 0;
        }
        .ttt .table img + p{
            text-align: center;
            margin: 10px 0 0;
        }
        .tst{
            color: #00b050;
            font-weight: 700;
            text-align: center;
            font-size: 1.25rem;
        }
    </style>

    <div class="portlet">
        <div class="portlet-title">
            <div class="caption">
                <x-portlet-breadcrumb :page="$page"></x-portlet-breadcrumb>
            </div>
            <div class="actions"></div>
        </div>
        {{--onsubmit="return checkWarrantyForm(this)" --}}
        <form action="/{{ request()->path() }}/../" method="post" class="warranty-form t-right">
            @csrf
            @method('PUT')
            <input type="text" name="_previous_" value="{{ request()->header('referer') }}" hidden>
            <input type="text" id ="warranty_id" name="id" value="{{ $data->id }}" hidden>
            <x-warranty-message :warranty="$data"></x-warranty-message>
            <table class="table s3">
                <tbody>
                <tr>
                    <td class="tth">{{ ts('warrantyid') }}</td>
                    <td class="ttd bg-light">{{ $data->warranty_id }}</td>
                    @if($data->precut_id)
                        <td class="tth">{{ ts('precutid') }}</td>
                        <td class="ttd bg-light">{{ getNPid($data->precut_id) }}</td>
                    @endif
                    {{--<td class="tth">{{ ts('azfs') }}</td>
                    <td class="ttd bg-light">{{ $data->precut_id ? ts('TCAZ') : ts('ptaz') }}</td>--}}
                    <td colspan="4"></td>
                </tr>
                <tr>
                    <td colspan="6" class="tst">{{ ts('CUSTOMERINFORMATION') }}</td>
                </tr>
                <tr>
                    <td class="tth">{{ ts('CUSTOMERNAME') }}</td>
                    <td class="ttd">
                        <input type="text" name="first_name" value="{{ $data->first_name ?: old('first_name') }}" class="form-control">
                    </td>
                    <td class="tth">{{ ts('phonenumber') }}</td>
                    <td class="ttd">
                        <div class=" input-group custom-control-inline">
                            <div class="input-group-prepend">
                                <select name="extension" class="form-control">
                                    @foreach(\App\Models\PhoneExtension::get(['id', 'phone_ext']) as $v)
                                        <option value="{{ $v->phone_ext }}" {{ ($data->extension == $v->phone_ext || old('phone_number')==$v->phone_ext ) ? 'selected' : '' }}>{{ $v->phone_ext }}</option>
                                    @endforeach
                                </select>
                            </div>
                            <input type="number" name="phone_number" value="{{ $data->phone_number ?: old('phone_number') }}" class="form-control">
                        </div>
                    </td>
                    <td class="tth">{{ ts('email') }}</td>
                    <td class="ttd">
                        <input type="text" name="email_address" value="{{ $data->email_address ?: old('email_address') }}" class="form-control">
                    </td>
                </tr>
                <tr>
                    <td class="tth">{{ ts('region') }}</td>
                    <td>
                        <select name="region_id" id="" class="form-control region_json" data-value="{{ $data->region_id ?: old('region_id') }}">
                            <option value="">{{ ts('select') }}</option>
                            <option value="{{ $data->region_id }}" selected>{{ ts('region_master.'.$data->region_id) }}</option>
                        </select>
                    </td>
                    <td class="tth">{{ ts('province') }}</td>
                    <td>
                        <select name="province_id" id="" class="form-control province_json" style="width: 100%" data-value="{{ $data->province_id ?: old('province_id') }}">
                            <option value="">{{ ts('select') }}</option>
                            <option value="{{ $data->province_id }}" selected>{{ ts('province_master.'.$data->province_id) }}</option>
                        </select>
                    </td>
                    <td class="tth">{{ ts('city') }}</td>
                    <td>
                        <select name="city" id="" class="form-control city_json" style="width: 100%" data-value="{{ $data->city ?: old('city') }}">
                            <option value="">{{ ts('select') }}</option>
                            <option value="{{ $data->city }}" selected>{{ ts('cities.'.$data->city) }}</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td class="tth">{{ ts('addresss') }}</td>
                    <td colspan="3">
                        <input type="text" name="address" value="{{ $data->address ?: old('address') }}" class="form-control">
                    </td>
                    <td class="tth"></td>
                    <td></td>
                    {{--<td class="tth">{{ ts('zip') }}</td>
                    <td>
                        <input type="text" name="zip" value="{{ $data->zip ?: old('zip') }}" class="form-control">
                    </td>--}}
                </tr>
                <tr>
                    <td colspan="6" class="tst">{{ ts('VEHICLEINFORMATION') }}</td>
                </tr>
                <tr>
                    <td class="tth">{{ ts('clnf') }}</td>
                    <td>
                        <select name="year_id" id="" class="form-control make_json_year" data-value="{{ $data->year_id ?: old('year_id') }}">
                            <option value="">{{ ts('select') }}</option>
                            <option value="{{ $data->year_id }}" selected>{{ $data->year_id }}</option>
                        </select>
                    </td>
                    <td class="tth">{{ ts('make') }}</td>
                    <td>
                        <select name="make_id" id="make_id" class="form-control make_id" style="width: 100%" data-value="{{ $data->make_id ?: old('make_id') }}">
                            <option value="">{{ ts('select') }}</option>
                            @if (!empty($makes))
                                @foreach($makes as $make)
                                    <option value="{{ $make->simplified_chiness_value }}" {{ $make->simplified_chiness_value == $make_name ? 'selected' : '' }}>{{ $make->simplified_chiness_value  }}</option>
                                @endforeach
                            @endif
                        </select>
                    </td>
                    <td class="tth">{{ ts('model') }}</td>
                    <td>
                        <select name="model_id" id="model_id" class="form-control model_id" data-value="{{ $data->model_id ?: old('model_id') }}">
                            <option value="">{{ ts('select') }}</option>
                            @if (!empty($models))
                                @foreach($models as $model)
                                    <option value="{{ $model->simplified_chiness_value }}" {{ $model->simplified_chiness_value == $model_name ? 'selected' : '' }}>{{ $model->simplified_chiness_value }}</option>
                                @endforeach
                            @endif
                        </select>
                        {{--<input type="text" name="model_id" value="{{ $data->model_id ?: old('model_id') }}" class="form-control" >--}}
                    </td>
                </tr>
                <tr>
                    <td class="tth">{{ ts('licenseplate') }}</td>
                    <td>
                        <input type="text" name="license_plate" value="{{ $data->license_plate ?: old('license_plate') }}" class="form-control" >
                    </td>
                    <td class="tth">{{ ts('vinnumber') }}</td>
                    <td colspan="3">
                        <input type="text" name="vin_number" value="{{ $data->vin_number ?: old('vin_number') }}" class="form-control" >
                    </td>
                </tr>
                <tr>
                    <td colspan="6" class="tst">{{ ts('INSTALLATIONINFORMATION') }}</td>
                </tr>
                <tr>
                    @if(@$data->precut_id)
                        <td class="tth">{{ ts('precutid') }}</td>
                        <td class="ttd">
                            <input type="text" name="precut_id" value="{{ $data->precut_id }}" hidden>
                            <div class="input-group">
                                <input type="number" value="{{ $data->user_id }}" name="user_id" hidden>
                                <select name="precut_id" id="" class="form-control" readonly disabled>
                                    <option value="{{ $data->precut_id }}">{{ getNPid($data->precut_id) }}</option>
                                </select>
                            </div>
                        </td>
                    @endif
                    <td class="tth">{{ ts('distributor') }}</td>
                    <td>
                        @if (session('admin.id') != 1 && empty($data->user_id))
                            <input type="text" class="form-control sdjc" disabled name="sdjc" value="{{ getDJc(session('admin.id')) }}">
                        @else
                            <input type="text" class="form-control sdjc" disabled name="sdjc" value="{{ getDJc($data->user_id) }}">
                        @endif

                    </td>
                    <td class="tth">{{ ts('ztmd') }}</td>
                    <td>
                        <div class="input-group">
                            <input type="number" value="{{ $data->user_id }}" name="user_id" hidden>
                            <select class="form-control roll_user_id" name="user_id" disabled data-value="{{ $data->user_id }}">
                                <option value="{{ $data->user_id }}">{{ getJc($data->user_id) }}</option>
                            </select>
                            @if(empty($data->precut_id) && empty($data->precut_id))
                                <div class="input-group-prepend">
                                    <span class="btn btn-primary"  data-toggle="modal" data-target="#changeWarrantyUser">{{ ts('XG') }}</span>
                                </div>
                            @endif
                        </div>
                    </td>
                    @empty(@$data->precut_id)
                        <td colspan="2"></td>
                    @endif
                </tr>
                <tr>
                    <td class="tth">{{ ts('INSTALLERNAME') }}</td>
                    <td>
                        <input type="text" name="installer_name" value="{{ $data->installer_name ?: old('installer_name') }}" class="form-control" >
                    </td>
                    <td class="tth">{{ ts('INSTALLATIONDATE') }}</td>
                    <td>
                        <div class="input-group">
                            <input type="text" name="installation_date" value="{{ fdate(@$data->installation_date) ?: fdate(old('installation_date')) }}" class="form-control flatpickr-ymdhis" data-max="{{ date('Y-m-d') }}">
                            <div class="input-group-prepend">
                                <span class="btn btn-primary fkr-clear"><i class="fa fa-times"></i></span>
                            </div>
                        </div>
                    </td>
                    <td class="tth">{{ ts('INSTALLERPRICE') }}</td>
                    <td>
                        <input type="text" name="installation_price" value="{{ $data->installation_price ?: old('installation_price') }}" class="form-control" >
                    </td>
                </tr>
                {{--                @empty(@$data->precut_id)--}}
                <tr>
                    <td colspan="6" class="tst">{{ ts('INSTALLATIONPICTURE') }}</td>
                </tr>
                <tr>
                    <td colspan="6" class="ttt">
                        <x-warranty-image-upload :warranty="$data"></x-warranty-image-upload>
                        {{--@include('admin.components.aztp-tb')--}}
                    </td>
                </tr>
                {{--                @endif--}}
                <tr>
                    <td colspan="6" class="tst">{{ ts('VEHICLECOVERAGE') }}</td>
                </tr>
                <tr>
                    <td colspan="6" class="ttt p-0">
                        @empty($data->precut_id)
                            @include('admin.subview.coverage', ['coverages' => @getWVCByID($data->id), 'is_precut'=>@$data->pre_id ? 1 : 0])
                        @else
                            <x-new-precut-fpart :warranty="$data"></x-new-precut-fpart>
                        @endif
                    </td>
                </tr>
                <tr>
                    <td colspan="6" class="tst">{{ ts('BCSM') }}</td>
                </tr>
                <tr>
                    <td colspan="6" height="auto">
                        <textarea name="additional_comments"  height="auto" rows="6" id="additional_comments" class="form-control">{{ @$data->additional_comments ?: old('additional_comments') }}</textarea>
                    </td>
                </tr>
                </tbody>
            </table>

            <div class="mt-4">
                <button type="button" class="btn btn-info" onclick="saveWarranty(this)">{{ ts('store') }}</button>
                <button class="btn btn-primary ml-5">{{ ts('SUBMIT') }}</button>
                <button type="button" class="btn btn-danger ml-5" onclick="refuse(this)">拒绝</button>
            </div>
        </form>
        @php

            if (session('admin.id') != 1 && empty($data->user_id)) {
                $data->user_id = session('admin.id');
            }
        $wUser = getUser($data->user_id);
        $wDealers = [];
        $Dealers = [];
        $wDTId = 0;
        if (empty($wUser->id)) {
        } else {
            if ($wUser->user_type==2) {
                $Dealers = getDealers($wUser->id);
                if(session('admin.warranty_self')==0){
                    $wDealers[] = $wUser;
                }
                $wDTId = $wUser->id;
            }
            if ($wUser->user_type==3) {
                $Dealers = getDealers($wUser->creator_id);
                if(session('admin.warranty_self')==0){
                    $wDealers[] = getUser($wUser->creator_id);
                }
                $wDTId = $wUser->creator_id;
            }
            foreach ($Dealers as $v) {
                $wDealers[] = $v;
            }
        }
        @endphp
        <div class="modal fade modal-s1" id="changeWarrantyUser">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title font-green">{{ ts('xgqd') }}</h5>
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                    </div>
                    <div class="modal-body">
                        @if(session('admin.user_type')==1)
                            <div class="form-item">
                                <div class="form-label" required="">{{ ts('xzsd') }}</div>
                                <div class="form-input">
                                    <select class="form-control" name="distributor_id">
                                        @if(session('admin.user_type')==1)
                                            @foreach(getDistributors() as $v)
                                                <option value="{{ $v->id }}" {{ $wDTId == $v->id ? 'selected' : '' }}>{{ $v->abbr ?: $v->company_name }}</option>
                                            @endforeach
                                        @endif
                                    </select>
                                </div>
                            </div>
                        @endif

                        <div class="form-item">
                            <div class="form-label" required="">{{ ts('xzjxs') }}</div>
                            <div class="form-input">
                                <select class="form-control" name="dealer_id" placeholder="">
                                    @foreach($wDealers as $v)
                                        <option value="{{ $v->id }}" {{ $data->user_id == $v->id ? 'selected' : '' }}>{{ $v->abbr ?: $v->company_name }}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                        <div class="py-3 text-center">
                            <button type="button" class="btn btn-primary" onclick="handleChangeWarrantyUser()">{{ ts('confirm') }}</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal fade modal-s1" id="changePrecut">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title font-green">{{ ts('XGTCID') }}</h5>
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                    </div>
                    <div class="modal-body">
                        <div class="form-item">
                            <div class="form-label" required="">{{ ts('XZTC') }}</div>
                            <div class="form-input">
                                <select class="form-control" name="dealer_id" placeholder="">
                                    @if(session('admin.user_type') == 2)
                                        <option value="{{ @$data->precut_id }}">{{ getPPId(@$data->precut_id) }}</option>
                                        @foreach(getDPDs(session('admin.id')) as $v)
                                            <option value="{{ @$v->id }}">{{ @$v->precut_id }}</option>
                                        @endforeach
                                    @endif
                                </select>
                            </div>
                        </div>
                        <div class="py-3 text-center">
                            <button type="button" class="btn btn-primary" onclick="handleChangePrecut()">{{ ts('confirm') }}</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

@endsection

@section('script')
    <script>
        $(function () {
            $("#make_id").select2();
            $("#make_id").on("change", function () {
                console.log($(this).val());
                request.get('/admin/setting-model/getModels', {
                    params: {
                        make_name: $(this).val(),
                    }
                })
                    .then( function(d) {
                        getModels(d);
                    })
                    .catch(function (d) {
                        getModels(d);
                    })
            });
        });
        function getModels(d) {
            if (d != '' || Array.isArray(d) && d.length >= 1) {
                let selectHtml = '<option value="">请选择</option>';
                for (let i in d) {
                    if (typeof d[i] === 'undefined' || typeof d[i]['simplified_chiness_value'] === 'undefined') {
                        continue;
                    }
                    selectHtml += '<option value="' + d[i]['simplified_chiness_value'] + '">' + d[i]['simplified_chiness_value'] + '</option>';
                }
                $("#model_id").empty().append(selectHtml);
            } else {
                $("#model_id").empty();
            }
        }

        $(document).ready(function () {
            $(".make_json_make,.province_json,.city_json,.warranty_part， #make_id, #model_id").select2();
            $(".roll_user_id").select2();
        });

        function saveWarranty(dom) {
            $(dom).attr('disabled', true);
            var form = formObjV2('.warranty-form');
            form.t = 'save';
            var url = '/admin/ajax/warranty';
            request.post(url, form)
                .then(function (d) {
                    swal('', d.msg || '保存成功！', 'success')
                        .then(function (d) {
                            // window.location.assign(window.location.href)
                            window.location.assign('./../')
                        })
                })
                .catch(function (d) {
                    alert(d.msg || '保存失败');
                    $(dom).attr('disabled', false);
                })
        }

        function refuse(dom){
            $(dom).attr('disabled', true);
            var form = formObjV2('.warranty-form');
            if (!form.additional_comments) {
                alert('补充说明不能为空');
                $(dom).attr('disabled', false);
                return false;
            }
            form.t = 'refuse';
            var url = '/admin/ajax/warranty';
            request.post(url, form)
                .then(function (d) {
                    swal('', d.msg || '操作成功！', 'success')
                        .then(function (d) {
                            window.location.assign('./../')
                        })
                })
                .catch(function (d) {
                    alert(d.msg || '操作失败');
                    $(dom).attr('disabled', false);
                })
        }
        function getPrecutDetail() {
            var id = $('[name="precut_id"]').val()
            request.get('/admin/ajax/getPVC', {
                params: {
                    id: id,
                }
            }).then(function (d) {
                var obj = d.data;
                var jxs = d.jxs;
                initCoverageRowsV2(obj);
                $('[name="user_id"]').val(jxs.user_id)
                $('input[name="sdjc"]').val(jxs.sdmc)
                $('select[name="user_id"]').empty().append(makeSelectOptionsOnly([{label: jxs.jxsmc, value: jxs.user_id}]))
            })
                .catch(function (d) {

                })
        }
        $(document).ready(function () {
            /*$('body').on('change', '.warranty-form input', function () {
                var fields = [
                    'first_name',
                    'extension',
                    'phone_number',
                    'email_address',
                    'region_id',
                    'province_id',
                    'city',
                    'address',
                    'year_id',
                    'make_id',
                    'model_id',
                    'license_plate',
                    'vin_number',
                    'user_id',
                    'installer_name',
                    'installation_date',
                    'installation_price'
                ];
                var name = $(this).attr('name');
                if (name && fields.indexOf(name)>=0) {
                    if ($(this).val()) {
                    } else {
                        var text = $(this).parents('td').prev('td').text();
                        if ($(this).parents('td').find('.error_info').length < 1) {
                            $(this).parents('td').append('<div class="error_info"></div>')
                        }
                        $(this).focus();
                        $(this).parents('td').find('.error_info').text(text + '不能为空！').addClass('active')
                    }
                }
                console.log($(this).attr('name'))
            });*/
            if ($('[name="precut_id"]').val()) {

            } else if ($('[name="pre_id"]').val()) {
                getPrecutDetail();
            } else {
                initCoverageRowsV2();
            }
        })
        function handleChangePrecut() {

        }
    </script>
@endsection